from yacf.Backends.DotBackend.Writers.DotWriter import DotWriter, DGraphWriter
import subprocess
import os
from cStringIO import StringIO

import config
import sys


class DotDebugTool(object):
        """Display the tree with xdot, highlighting the given nodes 

            :param highlight: Nodes to highlight, default: None

            .. warning::
                    This tool requires xdot in *config.WORKDIR*, and uses a temporary
                    storage for the dot file
        """
        def __init__(self, highlight = None):
            """Instantiates the debug tool
                 
                :param highlight: Nodes to highlight, default: None
            """
            self.tmpfile = '/tmp/dotfile.dot'
            self.MAX_LINES = 20000

            if type(highlight) != type([]) and highlight:
                self.highlight = [highlight]
            else:
                self.highlight = highlight

        def apply(self,node):
            """Display the given node (or list of nodes)
                 
                :param node: Node or list of nodes to begin display
            """
            if type(node) == type([]):
                for elem in node:
                    self.debug_node(elem)
            else:
                self.debug_node(node)

        def debug_node(self, node):
            """Display the given node (or list of nodes)
                 
                :param node: Node to display
            """
            DotWriter(filename = self.tmpfile, highlight = self.highlight).visit(node)
            size =  len(open(self.tmpfile).readlines())
            if not (size > self.MAX_LINES):
                try:
                  p = subprocess.Popen("python " + config.WORKDIR + "/yacf/utils/xdot.py " + "/tmp/dotfile.dot", shell=True)
                  sts = os.waitpid(p.pid, 0)[1]
                except:
                     print "DotDebugTool:::: Cannot launch xdot"
            else:
                print "DotDebugTool:::: AST Too big to show"



class DGraphDebug(DotDebugTool):
        def debug_node(self, depGraph):
            """Display the given node (or list of nodes)
                 
                :param node: Node to display
            """
            DGraphWriter(filename = self.tmpfile, highlight = self.highlight).apply(depGraph)
            size =  len(open(self.tmpfile).readlines())
            if not (size > self.MAX_LINES):
                try:
                  p = subprocess.Popen("pythonw2.7 " + config.WORKDIR + "/yacf/utils/xdot.py " + "/tmp/dotfile.dot", shell=True)
                  sts = os.waitpid(p.pid, 0)[1]
                except:
                  print "DGraphDebug:::: Cannot launch xdot"
            else:
                print "DotDebugTool:::: AST Too big to show"
